package com.deli.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.deli.springboot.entity.BorrowHistory;
import com.deli.springboot.entity.Comment;
import com.deli.springboot.entity.ResultDo;
import com.deli.springboot.service.BorrowHistoryServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 借阅与还书
 */
@RestController
public class BorrowHistoryController {

    private static final Logger logger = LoggerFactory.getLogger(BorrowHistoryController.class);
    @Autowired
    private BorrowHistoryServiceImpl borrowHistoryService;

    /**
     * 修改借书记录（还书）
     *
     * @param userId 用户id
     * @return
     */
    @RequestMapping(value = "borrowHistory/update")
    public String update(@RequestParam(value = "userId") Long userId) {
        ResultDo<Integer> result = new ResultDo<>();
        if (null == userId && userId < 0) {
            result.setErrorDescription("输入要还的书id：" + userId + " 有误");
            return JSON.toJSONString(result);
        }

        Integer update = borrowHistoryService.update(userId);
        if (update == 0) {
            result.setSuccess(false);
            result.setErrorDescription("没有要还的书");
            return JSON.toJSONString(result);
        }
        result.setSuccess(true);
        return JSON.toJSONString(result);
    }

    /**
     * 新增借书记录
     *
     * @param borrowHistory 借书记录对象的json字符串
     * @return
     */
    @RequestMapping(value = "borrowHistory/add")
    public String add(@RequestParam(value = "borrow_history") BorrowHistory borrowHistory) {
        logger.info("go to BorrowHistoryController  addMethod borrowHistory param is " + borrowHistory);
        ResultDo<Integer> result = new ResultDo();
        if (borrowHistory.getUserId() != null && borrowHistory.getUserId() > 0) {
            //存在useriD，查询是否有在借记录
            String byUserId = this.findByUserId(borrowHistory.getUserId());
            result = JSON.parseObject(byUserId, ResultDo.class);
            if (result.getData()==1){
                //存在记录
                return JSON.toJSONString(result);
            }
            Integer add = borrowHistoryService.doAdd(borrowHistory);
            if (add == 1) {
                result.setSuccess(true);
                result.setData(1);
                return JSON.toJSONString(result);
            }
            result.setSuccess(false);
            result.setErrorDescription("新增借阅记录失败");
            return JSON.toJSONString(result);

        }
        return JSON.toJSONString(result);
    }

    /**
     * 根据用户id查询该用户是否有在借的借书记录
     *
     * @param userId 用户id
     * @return
     */
    public String findByUserId(Long userId) {
        ResultDo<Integer> result = new ResultDo<>();
        if (null == userId && userId < 0) {
            result.setErrorDescription("输入的用户id：" + userId + " 有误");
            return JSON.toJSONString(result);
        }
        BorrowHistory borrowHistory = new BorrowHistory();
        borrowHistory.setUserId(userId);
        borrowHistory.setStatus(1);//未归还
        List<BorrowHistory> borrowHistoryList = borrowHistoryService.findByExample(borrowHistory);

        if (borrowHistoryList != null && borrowHistoryList.size() > 0) {
            //存在记录
            result.setData(1);
            result.setErrorDescription("存在在借记录");
            result.setSuccess(false);
            return JSON.toJSONString(result);
        }
        result.setData(0);//不存在在借记录
        result.setSuccess(true);
        return JSON.toJSONString(result);
    }
}

